﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using OnTheWay.Functions;
using Models;
using LocalService;
using FileService;
using OnTheWay.Enums;
using OnTheWay.Exceptions;

namespace PcBackgroundMgr.Functions
{
    /// <summary>
    /// ExhibitUnitModelCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ExhibitUnitModelCtrl : ExhibitUnitModelCtrlBase
    {
        public ExhibitUnitModelCtrl()
        {
            InitializeComponent();
        }
    }


    [DesignTimeVisible(false)]
    public class ExhibitUnitModelCtrlBase : FunctionCtrlModel<ExhibitUnit>
    {
        private Exhibit mExhibit;

        public ExhibitUnitModelCtrlBase()
            : base(LocalFactory.ExhibitUnitLocal)
        {
            ExhibitUnitTypes = Enum.GetValues(typeof(ExhibitUnitTypeEnum));
        }

        public void SetExhibit(Exhibit model)
        {
            mExhibit = model;
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        protected override bool OnCanSave(ExhibitUnit model)
        {
            model.Name = mExhibitUnitType.ToString();// "ExhibitUnit";//model.Author;
            if (base.OnCanSave(model))
            {
                if (mExhibitUnitType == ExhibitUnitTypeEnum.视频)
                {
                    if (String.IsNullOrWhiteSpace(model.MooMaUrl))
                    {
                        MessageStatus = MessageStatusEnum.Warning;
                        Message = "请上传视频封面";
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        public override ExhibitUnit OnInitializeModel(ExhibitUnit model)
        {
            if (model == null)
            {
                model = new ExhibitUnit();
                this.Title = "添加内容";
                if (mExhibit != null && mExhibit.ClassEnum == ExhibitClassEnum.视频)
                {
                    this.Title = "添加" + mExhibit.ClassEnum.ToString();
                    this.ExhibitUnitType = ExhibitUnitTypeEnum.视频;
                }
            }
            else
            {
                this.Title = "内容不支持编辑";
                if (this.Content is FrameworkElement)
                {
                    (this.Content as FrameworkElement).IsEnabled = false;
                }
                //this.Title = "编辑内容";
                //this.mExhibitUnitType = ExhibitUnitTypeEnum.图片;
                //if (String.IsNullOrWhiteSpace(model.UnitUrls))
                //{
                //    string[] urls = model.UnitUrls.Split(',');
                //    this.MooMaImage1 = FileFactory.GetUrl(urls[0], FolderEnum.IMAGE);
                //    if (urls.Length > 1)
                //    {
                //        this.MooMaImage2 = FileFactory.GetUrl(urls[1], FolderEnum.IMAGE);
                //    }
                //}
            }
            return model;
        }

        #region 页面可绑定数据

        /// <summary>
        /// 封面图片路径1(包括视频)
        /// </summary>
        private string mMooMaPath1 = "";
        public string MooMaPath1
        {
            get { return mMooMaPath1; }
            set
            {
                if (value != mMooMaPath1 && MooMaPercent1 < 0)
                {
                    mMooMaPath1 = value;
                    MooMaPercent1 = 0;
                    PostTask(() =>
                    {
                        try
                        {
                            string url = Model.ID.ToString() + "1";
                            FileFactory.UploadFile(mMooMaPath1, url, FolderEnum.TEMP,
                            (int percent, int speed, long sended, long Length) =>
                            {
                                Dispatcher.Invoke(new Action(() =>
                                {
                                    MooMaPercent1 = percent / 100f;
                                }));
                            });
                            Dispatcher.Invoke(new Action(() =>
                            {
                                MooMaPercent1 = -1;
                                if (mExhibitUnitType == ExhibitUnitTypeEnum.图片)
                                {
                                    Model.UnitUrls = url;
                                    if (!String.IsNullOrWhiteSpace(mMooMaPath2))
                                    {
                                        Model.UnitUrls += "," + Model.ID.ToString() + "2";
                                    }
                                }
                                else
                                {
                                    Model.MooMaUrl = url;
                                }
                                MessageStatus = MessageStatusEnum.Success;
                                Message = "封面上传完成";
                            }));
                        }
                        catch (System.Exception ex)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                MooMaPercent1 = -1;
                                Model.MooMaUrl = "";
                                MessageStatus = MessageStatusEnum.Error;
                                Message = "封面上传失败：" + ExceptionHandle.GetMessage(ex);
                            }));
                        }
                    });
                }
            }
        }

        /// <summary>
        /// 封面图片路径2
        /// </summary>
        private string mMooMaPath2 = "";
        public string MooMaPath2
        {
            get { return mMooMaPath2; }
            set
            {
                if (value != mMooMaPath2 && MooMaPercent2 < 0)
                {
                    mMooMaPath2 = value;
                    MooMaPercent2 = 0;
                    PostTask(() =>
                    {
                        try
                        {
                            string url = Model.ID.ToString() + "2";
                            FileFactory.UploadFile(mMooMaPath2, url, FolderEnum.TEMP,
                            (int percent, int speed, long sended, long Length) =>
                            {
                                Dispatcher.Invoke(new Action(() =>
                                {
                                    MooMaPercent2 = percent / 100f;
                                }));
                            });
                            Dispatcher.Invoke(new Action(() =>
                            {
                                MooMaPercent2 = -1;
                                if (mExhibitUnitType == ExhibitUnitTypeEnum.图片)
                                {
                                    if (!String.IsNullOrWhiteSpace(mMooMaPath1))
                                    {
                                        Model.UnitUrls = Model.ID.ToString() + "1," + url;
                                    }
                                    else
                                    {
                                        Model.UnitUrls = url;
                                    }
                                }
                                else
                                {
                                    Model.UnitUrls = url;
                                }
                                MessageStatus = MessageStatusEnum.Success;
                                Message = "封面上传完成";
                            }));
                        }
                        catch (System.Exception ex)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                MooMaPercent1 = -1;
                                MessageStatus = MessageStatusEnum.Error;
                                Message = "封面上传失败：" + ExceptionHandle.GetMessage(ex);
                            }));
                        }
                    });
                }
            }
        }
        /// <summary>
        /// 展示类型
        /// </summary>
        private ExhibitUnitTypeEnum mExhibitUnitType = ExhibitUnitTypeEnum.图片;
        public ExhibitUnitTypeEnum ExhibitUnitType
        {
            get { return mExhibitUnitType; }
            set
            {
                mExhibitUnitType = value;
                PropertyNotifyChanged("ExhibitUnitType");
                if (Model != null)
                {
                    Model.Type = (int)value;
                }
            }
        }
        /// <summary>
        /// 提供选择类型列表
        /// </summary>
        public Array ExhibitUnitTypes { get; set; }

        #endregion

        #region 依赖属性
        /// <summary>
        /// 封面图片
        /// </summary>
        private string _MooMaImage1;

        public string MooMaImage1
        {
            get { return _MooMaImage1; }
            set { _MooMaImage1 = value; PropertyNotifyChanged("MooMaImage1"); }
        }
        /// <summary>
        /// 上传百分比
        /// </summary>
        private float _MooMaPercent1 = -1;
        public float MooMaPercent1
        {
            get { return _MooMaPercent1; }
            set { _MooMaPercent1 = value; PropertyNotifyChanged("MooMaPercent1"); }
        }

        /// <summary>
        /// 封面图片
        /// </summary>
        private string _MooMaImage2;

        public string MooMaImage2
        {
            get { return _MooMaImage2; }
            set { _MooMaImage2 = value; PropertyNotifyChanged("MooMaImage2"); }
        }
        /// <summary>
        /// 上传百分比
        /// </summary>
        private float _MooMaPercent2 = -1;
        public float MooMaPercent2
        {
            get { return _MooMaPercent2; }
            set { _MooMaPercent2 = value; PropertyNotifyChanged("MooMaPercent2"); }
        }

        #endregion
    }

}
